package main

import (
	"fmt"
	underscore "github.com/ahl5esoft/golang-underscore"
)

type testModel struct {
	ID   int
	Name string
}

func main() {
	// 判断是否存在
	testData := []testModel{
		{ID: 1, Name: "one"},
		{ID: 1, Name: "two"},
		{ID: 1, Name: "three"},
	}
	ok := underscore.Chain(testData).All(func(r testModel, _ int) bool {
		return r.ID == 1
	})
	fmt.Printf("ID == 1? %t \n", ok)
	// ID == 1? true

	ok1 := underscore.Chain(testData).AllBy(map[string]interface{}{
		"name": "one",
	})
	fmt.Printf("Name:one? %t \n", ok1)
	// Name:one? false

	// distinct
	src := []int{1, 2, 1, 4, 1, 3}
	dst := make([]int, 0)
	underscore.Chain(src).Distinct(func(n, _ int) (int, error) {
		//return n % 2, nil
		return n, nil
	}).Value(&dst)
	fmt.Printf("src distinct: %v \n", dst)
	// src distinct: [1 2 4 3]

	// distinct by
	dst2 := make([]testModel, 0)
	underscore.Chain(testData).DistinctBy("ID").Value(&dst2)
	fmt.Printf("testModel distinct by name: %v \n", dst2)
	// testModel distinct by name: [{1 one}]

	// memo
	dst3 := make([]int, 0)
	underscore.Chain([]int{1, 21}).Aggregate(make([]int, 0), func(memo []int, n, _ int) []int {
		memo = append(memo, n)
		memo = append(memo, n+10)
		return memo
	}).Value(&dst3)
	fmt.Printf("memo: %v \n", dst3)
	// memo: [1 11 21 31]

	// All 判断所有元素 id == 1
	ok2 := underscore.Chain(testData).All(func(r testModel, _ int) bool {
		return r.ID == 1
	})
	fmt.Printf("all: %t \n", ok2)

	// AllBy
	ok3 := underscore.Chain(testData).AllBy(map[string]interface{}{
		"name": "one",
	})
	fmt.Printf("allBy: %t \n", ok3)

	// Any 只要有一个满足,就为 true
	// AnyBy
	ok4 := underscore.Chain([]testModel{
		{ID: 1, Name: "one"},
		{ID: 2, Name: "two"},
		{ID: 3, Name: "three"},
	}).Any(func(r testModel, _ int) bool {
		return r.ID == 0
	})
	fmt.Printf("Any: %t \n", ok4)

}
